package com.mmlc.meimei.lichengkongjian.classify;

import android.app.AlertDialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;

public class ModeInvoiceSelectActivity extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_mode_invoice_select;
    }

    TextView tv_company;
    CheckBox checkbox_no, checkbox_personal, checkbox_company;
    ArrayList<CheckBox> checkBoxes = new ArrayList<>();

    @Override
    protected void initView() {
        tv_company = ViewUtils.getView(activity, R.id.tv_company);

        checkbox_no = ViewUtils.getView(activity, R.id.checkbox_no);
        checkbox_personal = ViewUtils.getView(activity, R.id.checkbox_personal);
        checkbox_company = ViewUtils.getView(activity, R.id.checkbox_company);

        checkBoxes.add(checkbox_no);
        checkBoxes.add(checkbox_personal);
        checkBoxes.add(checkbox_company);

        if (selectedMode != -1) {
            checkBoxes.get(selectedMode).setChecked(true);
            if (selectedMode == 2 && !ViewUtils.isEmpty(companyName))
                tv_company.setText("开具公司发票（" + (companyName.length() <= 8 ? companyName : companyName.substring(0, 8) + "...") + "）");
        }
    }

    @Override
    protected void initExtra() {
        super.initExtra();
        selectedMode = getIntent().getIntExtra("mode", -1);
        companyName = getIntent().getStringExtra("companyName");
    }

    @Override
    protected void initEvent() {
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.tv_verify).setOnClickListener(this);

        ViewUtils.getView(activity, R.id.rl_no).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.rl_personal).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.rl_company).setOnClickListener(this);
    }

    int selectedMode = -1;
    String companyName = "";

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                setResult(200);
                finish();
                break;
            case R.id.rl_no:
                selectMode(0);
                break;
            case R.id.rl_personal:
                selectMode(1);
                break;
            case R.id.rl_company:
                initInputDialog();
                break;
            case R.id.tv_verify:
                if (selectedMode == -1) {
                    setResult(200, new Intent().putExtra("mode", selectedMode));
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("mode", selectedMode);
                    intent.putExtra("companyName", companyName);
                    setResult(200, intent);
                }
                finish();
                break;
        }
    }

    AlertDialog inputDialog;
    private EditText edt_input;

    private void initInputDialog() {
        inputDialog = new AlertDialog.Builder(activity).create();
        inputDialog.setView(getLayoutInflater().inflate(R.layout.dialog_input, null));
        inputDialog.show();
        inputDialog.getWindow().setGravity(Gravity.CENTER);
        inputDialog.getWindow().setLayout(android.view.WindowManager.LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        inputDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.dialog_input, null));

        edt_input = (EditText) inputDialog.findViewById(R.id.edt_input);
        if (!ViewUtils.isEmpty(companyName)) {
            edt_input.setText(companyName);
        }

        ViewUtils.getView(inputDialog, R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputDialog.hide();
            }
        });
        ViewUtils.getView(inputDialog, R.id.tv_verify).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ViewUtils.isEmpty(edt_input.getText().toString()))
                    Toast.makeText(context, "请输入公司名称", Toast.LENGTH_SHORT).show();
                else {
                    companyName = edt_input.getText().toString().replace(" ", "");
                    selectMode(2);
                    tv_company.setText("开具公司发票（" + (companyName.length() <= 8 ? companyName : companyName.substring(0, 8) + "...") + "）");

                    inputDialog.hide();
                }
            }
        });
    }

    private void selectMode(int i) {
        if (selectedMode == i) {
            checkBoxes.get(i).setChecked(false);
            selectedMode = -1;
        } else {
            if (selectedMode != -1) {
                checkBoxes.get(selectedMode).setChecked(false);
            }
            checkBoxes.get(i).setChecked(true);
            selectedMode = i;
        }
    }
}
